﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.IO;
using RealState.Utils;

namespace RealState.W
{
    public partial class LoginCP : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {

        }

        protected void Button1_Click(object sender, EventArgs e)
        {
            Session.Clear();
            if (Check())
            {
                Session.Add("USERID", txtUsername.Text);
                Session.Timeout = 20;
                Response.Redirect("~/AdminPanel/Default.aspx");
            }
            else
            {
                Response.Write("Sai tên đăng nhập và mật khẩu");
                return;
            }
        }
        public bool Check()
        {
            FileStream fileStream;
            string line = null;
            try
            {
                fileStream = new FileStream(Server.MapPath("~") + "ReadState.dat", FileMode.Open, FileAccess.Read, FileShare.Read);
                StreamReader read = new StreamReader(fileStream);
                while ((line = read.ReadLine()) != null)
                {
                    string userName = line.Split(',')[0].ToString();
                    string passWord = line.Split(',')[1].ToString();
                    if (userName.Equals(txtUsername.Text) && passWord.Equals(Converter.MD5Encrypt(txtPassword.Text)))
                    {
                        return true;
                    }
                }
                read.Close();

            }
            catch (Exception ex)
            {

            }
            return false;
        }
    }
}
